package com.xsy.android.compose.wandroid.ui.main

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import androidx.paging.compose.collectAsLazyPagingItems
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.PlayLoading
import com.xsy.android.compose.wandroid.logic.model.PlaySuccess
import com.xsy.android.compose.wandroid.ui.main.compose.BottomNavigation
import com.xsy.android.compose.wandroid.ui.main.compose.BottomNavigationItem
import com.xsy.android.compose.wandroid.ui.page.home.HomePage
import com.xsy.android.compose.wandroid.ui.page.home.HomePageViewModel
import com.xsy.android.compose.wandroid.ui.page.login.LoginViewModel
import com.xsy.android.compose.wandroid.ui.page.login.LogoutDefault
import com.xsy.android.compose.wandroid.ui.page.mine.ProfilePage
import com.xsy.android.compose.wandroid.ui.page.project.OfficialViewModel
import com.xsy.android.compose.wandroid.ui.page.project.ProjectPage
import com.xsy.android.compose.wandroid.ui.page.project.ProjectViewModel

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun MainPage(
    modifier: Modifier = Modifier,
    actions: NavigationUtil,
    current: Int,
    currentChanged: (Int) -> Unit,

    ) {
    val tabs = CourseTabs.entries.toTypedArray()
    Scaffold(
        modifier = modifier.background(color = MaterialTheme.colorScheme.primary),
        bottomBar = {
            BottomAppBar(modifier = modifier.background(color = MaterialTheme.colorScheme.primary)) {
                BottomNavigation(modifier = modifier.background(color = MaterialTheme.colorScheme.primary)) {
                    tabs.forEachIndexed { index, tab ->
                        BottomNavigationItem(
                            modifier = modifier
                                .background(color = MaterialTheme.colorScheme.primary)
                                .weight(1f)
                                .clickable {
                                    currentChanged(index)
                                }, iconId = if (index == current) {
                                tab.selectIcon
                            } else {
                                tab.icon
                            }, title = stringResource(id = tab.title)
                        )
                    }
                }
            }
        }
    ) { innerPadding ->
        when (current) {

            0 -> {
                val viewModel: HomePageViewModel = viewModel()
                val bannerData by viewModel.bannerState.observeAsState(initial = PlayLoading)
                val lazyPagingItems = viewModel.articleResult.collectAsLazyPagingItems()
                HomePage(
                    modifier = modifier.padding(innerPadding),
                    bannerData = bannerData,
                    lazyPagingItems = lazyPagingItems,
                    {
                        if (bannerData !is PlaySuccess<*>) {
                            viewModel.getBanner()
                        }
                        if (lazyPagingItems.itemCount <= 0) {
                            viewModel.getHomeArticle()
                        }
                    }) { article ->
                    actions.enterArticle(article)
                }
            }

            1 -> {
                val viewModel = viewModel<ProjectViewModel>()
                val lazyPagingItems = viewModel.articleResult.collectAsLazyPagingItems()
                val tree by viewModel.treeLiveData.observeAsState(PlayLoading)
                val treePosition by viewModel.position.observeAsState(initial = 0)
                ProjectPage(
                    modifier = modifier.padding(innerPadding),
                    tree = tree,
                    position = treePosition,
                    lazyPagingItems = lazyPagingItems,
                    {
                        viewModel.getDataList()
                    },
                    {
                        viewModel.searchArticle(it)
                    },
                    {
                        viewModel.onPositionChanged(it)
                    }) {
                    actions.enterArticle(it)
                }
            }

            2 -> {
                val viewModel = viewModel<OfficialViewModel>()
                val lazyPagingItems = viewModel.articleResult.collectAsLazyPagingItems()
                val tree by viewModel.treeLiveData.observeAsState(PlayLoading)
                val treePosition by viewModel.position.observeAsState(initial = 0)
                ProjectPage(
                    modifier = modifier.padding(innerPadding),
                    tree = tree,
                    position = treePosition,
                    lazyPagingItems = lazyPagingItems,
                    {
                        viewModel.getDataList()
                    },
                    {
                        viewModel.searchArticle(it)
                    },
                    {
                        viewModel.onPositionChanged(it)
                    }) {
                    actions.enterArticle(it)
                }
            }

            3 -> {
                val viewModel: LoginViewModel = viewModel()
                val logoutState by viewModel.logoutSate.observeAsState(LogoutDefault)
                ProfilePage(
                    modifier = modifier.padding(innerPadding),
                    toLogin = actions.login,
                    enterArticle = actions.enterArticle,
                    logoutState = logoutState,
                ) {
                    viewModel.logout()
                }
            }
        }

    }
}

@Preview
@Composable
private fun PreviewMainPage() {
    val viewModel: HomeViewModel = viewModel()
    val position by viewModel.position.observeAsState()
    val navController = rememberNavController()
    val actions = remember(navController) { NavigationUtil(navController) }
    MainPage(actions = actions, current = position ?: 0) { tab ->
        viewModel.onPositionChange(tab)
    }
}


enum class CourseTabs(
    @StringRes val title: Int,
    @DrawableRes val icon: Int,
    @DrawableRes val selectIcon: Int
) {
    HOME_PAGE(
        R.string.home_page,
        R.mipmap.ic_nav_news_normal,
        R.mipmap.ic_nav_news_actived
    ),
    PROJECT(
        R.string.project,
        R.mipmap.ic_nav_tweet_normal,
        R.mipmap.ic_nav_tweet_actived
    ),
    OFFICIAL_ACCOUNT(
        R.string.official_account,
        R.mipmap.ic_nav_discover_normal,
        R.mipmap.ic_nav_discover_actived
    ),
    MINE(R.string.mine, R.mipmap.ic_nav_my_normal, R.mipmap.ic_nav_my_pressed)

}